﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace RSSPlugin
{
    public class RSSPlugin
    {

        /// <summary>
        /// Default constructor. For completeness sake.
        /// </summary>
        public RSSPlugin()
        {
            
        }

        private Dictionary<string, string> fields = null;

        private List<Dictionary<string, string>> childfields = new List<Dictionary<string, string>>();

        public Dictionary<string, string> Fields
        {
            get
            {
                return fields;
            }
            set
            {
                fields = value;
            }

        }

        public List<Dictionary<string, string>> ChildFields
        {
            get
            {
                return childfields;
            }
            set
            {
                childfields = value;
            }

        }

        


        public Panel CreateContent()
        {

            Panel newPanel = new Panel();

            Literal parent = new Literal();
            parent.Text = "Parent Item:" + "<br/>";

            Literal title = new Literal();
            title.Text = fields["title"] + "<br/>";

            Literal description = new Literal();
            description.Text = fields["description"] + "<br/>";

            newPanel.Controls.Add(parent);
            newPanel.Controls.Add(title);
            newPanel.Controls.Add(description);


            foreach (Dictionary<string, string> c in childfields)
            {

                Literal child = new Literal();
                child.Text = "Child Item:" + "<br/>";

                Literal childtitle = new Literal();
                childtitle.Text = c["title"] + "<br/>";

                Literal childdescription = new Literal();
                childdescription.Text = c["description"] + "<br/>";


                newPanel.Controls.Add(child);
                newPanel.Controls.Add(childtitle);
                newPanel.Controls.Add(childdescription);

            }


            /*
            //Response.Clear();
            //Response.ContentType = "text/xml";
            XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("rss");
            objX.WriteAttributeString("version", "2.0");
            objX.WriteStartElement("channel");

            objX.WriteElementString("title", fields["title"]);
            objX.WriteElementString("link", fields["link"]);
            objX.WriteElementString("description", fields["description"]);
            objX.WriteElementString("copyright", fields["copyright"]);
            objX.WriteElementString("ttl", fields["ttl"]);


            foreach (Dictionary<string, string> c in childfields)
            {
                objX.WriteStartElement("item");
                objX.WriteElementString("title", c["title"]);
                objX.WriteElementString("description", c["description"]);
                objX.WriteElementString("link", c["link"]);
                objX.WriteElementString("pubDate", c["pubDate"]);
                objX.WriteElementString("author", c["author"]);
                objX.WriteElementString("category", c["category"]);
                objX.WriteEndElement();
            }

            objX.WriteEndElement();
            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
            //Response.End();
             * 
             * */

            return newPanel;
            

        }


        #region iCMSPlugin Members

        public void Install()
        {

            throw new NotImplementedException();
        }

        public void Upgrade()
        {
            throw new NotImplementedException();
        }

        public void Uninstall()
        {
            throw new NotImplementedException();
        }

        public string Version()
        {
            return "0.1";
        }

        #endregion
    }
}
